<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>欢迎注册</title>
    <link href="css/register.css" rel="stylesheet">
</head>
<body>

<div class="form-div">
    <div class="reg-content">
        <h1>欢迎注册</h1>
        <span>已有帐号？</span> <a href="login.html">登录</a>
    </div>
    <form accept-charset="UTF-8" id="reg-form" >

        <table>

            <tr>
                <td>用户名</td>
                <td class="inputs">
                    <input name="username" type="text" id="username" placeholder="请尽量输入数字、字母、下划线">
                    <br>
                    <span id="username_err" class="err_msg" style="display: none">用户名已存在</span>
                    <span id="msg" class="msg" style="display: inline"></span>
                </td>

            </tr>

            <tr>
                <td>密码</td>
                <td class="inputs">
                    <input name="password" type="password" id="password">
                    <br>
                    <span id="password_err" class="err_msg" style="display: none">密码格式有误</span>
                </td>
            </tr>


            <tr>
                <td>验证码</td>
                <td class="inputs">
                    <input name="checkCode" type="text" id="checkCode">
                    <a href="#" id="changeImg" >
                        <img id="checkCodeImg" src="imgs/a.jpg">
                        看不清？</a>
                </td>

            </tr>
            <tr>
                <td>
                    <span></span>
                </td>
                <td>
                    <span style="color: red">提示信息：先刷新图片再输入验证码</span>
                </td>
            </tr>



        </table>


        <div class="buttons">
            <input value="注 册" type="button" id="reg_btn">
        </div>
        <br class="clear">
    </form>

</div>
<script src="https://cdn.jsdelivr.net/npm/axios@1.3.4/dist/axios.min.js"></script>  <!-- 更新 axios 版本 -->
<script>

    document.getElementById("reg_btn").onclick=function (){
        var username=document.getElementById("username").value;
        var password=document.getElementById("password").value;
        var checkCode=document.getElementById("checkCode").value;
        let errorMessageDoc = document.getElementById('msg');
        console.log(username)

        axios.get("http://localhost:8080/brand-case/user/register",{
            params: {
                username:username,
                password:password,
                checkCode:checkCode
            },
            // headers: {
            //     'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            // },
        }).then(function (response){
            if(response.data=="register_success"){
                errorMessageDoc.innerText="注册成功，30s后将跳转到登录页面";
                setTimeout(function (){
                    location.href="login.html";
                },30000);
            }
            else if(response.data=="Verification_code_error"){
                errorMessageDoc.innerText="验证码错误！";
                setTimeout(function (){
                    location.href="register.html";
                },3000);

            }
            else{
                errorMessageDoc.innerText="注册失败，用户名已经存在";
                setTimeout(function (){
                    location.href="register.html";
                },3000);
            }
        })
    }
    // 1. 给用户名输入框绑定失去焦点事件
    document.getElementById("username").onblur = function () {
        // 2. 发送 axios 请求
        var username = this.value;

        // 确保用户名不为空
        if (username.trim() === "") {
            document.getElementById("username_err").style.display = 'none';  // 为空时不显示错误
            return;
        }

        // 2.1. 发送请求
        axios.get("http://localhost:8080/brand-case/user/confirmUserByName", {
            params: {
                username: username
            }
        })
            .then(function (response) {
                console.log(response.data);
                console.log(typeof response.data)//boolean类型的，true（boolean类型）！="true"（string类型）
                console.log(typeof "true")//boolean类型的，true（boolean类型）！="true"（string类型）
                // 2.3. 获取响应
                if (response.data == "user_exists") {
                    // 用户名已存在，显示提示信息
                    document.getElementById("username_err").style.display = '';
                    console.log(1)

                } else {

                    // 用户名不存在，清除提示信息
                    document.getElementById("username_err").style.display = 'none';
                    console.log(2)
                }
            })
            .catch(function (error) {
                // 处理错误情况
                console.error('Error:', error);
                // 可添加错误提示
                document.getElementById("username_err").style.display = 'none';
            });
    };

    document.getElementById("changeImg").onclick =function ()  {
        document.getElementById("checkCodeImg").src = "/brand-case/checkCodeServlet?"+new Date().getMilliseconds();
    };

</script>

</body>
</html>